package com.example.demo.alex.service.serviceImpl;

import com.example.demo.alex.model.Person;
import com.example.demo.alex.model.Role;
import com.example.demo.alex.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName UserDetailsImpl
 * @Description TODO
 * @Author zjg
 * @Date 2018/12/2517:08
 * @Version 2.0
 */

public class UserDetailsImpl implements UserDetails {

    private String username;
    private String password;
    //用户对应的所有角色
    private List<Role> roles;

    @Autowired
    private RoleService roleService;

    public void setRoles(List<Role> roles){
        this.roles=roles;
    }

    public UserDetailsImpl(){

    }

    public UserDetailsImpl(Person person){

        this.username=person.getUserName();
        this.password=person.getPwd();
    }

    public UserDetailsImpl(Person person, List<Role> roles){
        this.username=person.getUserName();
        this.password=person.getPwd();
        this.roles=roles;
    }

    public List<Role> getRoles(){
        return roles;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities=new ArrayList<>();
        for(Role role : roles){
            authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    //判断账号是否过期，默认没有过期
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    //判断账号是否锁定，默认没有锁定
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    //判断信用凭证是否过期，默认没有过期
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    //判断账号是否可用，默认可用
    public boolean isEnabled() {
        return true;
    }
}
